import Vue from 'vue'
import VueRouter from 'vue-router'
// export const EventBus = new Vue();
Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
    meta: {
      title: "登录",
    },
  },
  {
    path: "/analysis",
    name: "analysis",
    component: () => import("../components/navbar.vue"),
    meta: {
      title: "Layout",
    },
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("../views/home/home.vue"),
        meta: {
          title: "首页",
        },
      },
      {
        path: "/cgrk",
        name: "cgrk",
        component: () => import("../views/kcgl/rkgl/cgrk.vue"),
        meta: {
          title: "采购入库",
        },
      },
      {
        path: "/cgthck",
        name: "cgthck",
        component: () => import("../views/kcgl/rkgl/cgthck.vue"),
        meta: {
          title: "采购退货出库(红入)",
        },
      },
      {
        path: "/pyrk",
        name: "pyrk",
        component: () => import("../views/kcgl/rkgl/pyrk.vue"),
        meta: {
          title: "盘盈入库",
        },
      },
      {
        path: "/zdrk",
        name: "zdrk",
        component: () => import("../views/kcgl/rkgl/zdrk.vue"),
        meta: {
          title: "涨吨入库",
        },
      },
      {
        path: "/qtrk",
        name: "qtrk",
        component: () => import("../views/kcgl/rkgl/qtrk.vue"),
        meta: {
          title: "其他入库",
        },
      },
      {
        path: "/xsck",
        name: "xsck",
        component: () => import("../views/kcgl/ckgl/xsck.vue"),
        meta: {
          title: "销售出库",
        },
      },
      {
        path: "/xsthrk",
        name: "xsthrk",
        component: () => import("../views/kcgl/ckgl/xsthrk.vue"),
        meta: {
          title: "销售退货入库",
        },
      },
      {
        path: "/pkck",
        name: "pkck",
        component: () => import("../views/kcgl/ckgl/pkck.vue"),
        meta: {
          title: "盘亏出库",
        },
      },
      {
        path: "/qtck",
        name: "qtck",
        component: () => import("../views/kcgl/ckgl/qtck.vue"),
        meta: {
          title: "其他出库",
        },
      },

      {
        path: "/kctb",
        name: "kctb",
        component: () => import("../views/kcgl/qtgl/kctb.vue"),
        meta: {
          title: "库存调拨",
        },
      },
      {
        path: "/cbtz",
        name: "cbtz",
        component: () => import("../views/kcgl/qtgl/cbtz.vue"),
        meta: {
          title: "成本调整",
        },
      },
      {
        path: "/kcpd",
        name: "kcpd",
        component: () => import("../views/kcgl/qtgl/kcpd.vue"),
        meta: {
          title: "库存盘点",
        },
      },
      {
        path: "/wlkc",
        name: "wlkc",
        component: () => import("../views/kcgl/jskc/wlkc.vue"),
        meta: {
          title: "物流库存",
        },
      },
      {
        path: "/wlpckc",
        name: "wlpckc",
        component: () => import("../views/kcgl/jskc/wlpckc.vue"),
        meta: {
          title: "物流批次库存",
        },
      },
      {
        path: "/ckwlkc",
        name: "ckwlkc",
        component: () => import("../views/kcgl/jskc/ckwlkc.vue"),
        meta: {
          title: "仓库物料库存",
        },
      },
      {
        path: "/xxkc",
        name: "xxkc",
        component: () => import("../views/kcgl/jskc/xxkc.vue"),
        meta: {
          title: "详细库存",
        },
      },
      {
        path: "/crkyhz",
        name: "crkyhz",
        component: () => import("../views/kcgl/crktj/crkyhz.vue"),
        meta: {
          title: "出入库月汇总",
        },
      },
      {
        path: "/crkrhz",
        name: "crkrhz",
        component: () => import("../views/kcgl/crktj/crkrhz.vue"),
        meta: {
          title: "出入库日汇总",
        },
      },
      {
        path: "/crkmx",
        name: "crkmx",
        component: () => import("../views/kcgl/crktj/crkmx.vue"),
        meta: {
          title: "出入库明细",
        },
      },
      {
        path: "/kh",
        name: "kh",
        component: () => import("../views/jcsj/kh.vue"),
        meta: {
          title: "客户",
        },
      },
      {
        path: "/gys",
        name: "gys",
        component: () => import("../views/jcsj/gys.vue"),
        meta: {
          title: "供应商",
        },
      },
      {
        path: "/ck",
        name: "ck",
        component: () => import("../views/jcsj/ck.vue"),
        meta: {
          title: "仓库",
        },
      },
      {
        path: "/wlfl",
        name: "wlfl",
        component: () => import("../views/jcsj/wlfl.vue"),
        meta: {
          title: "物料分类",
        },
      },
      {
        path: "/wl",
        name: "wl",
        component: () => import("../views/jcsj/wl.vue"),
        meta: {
          title: "物料",
        },
      },
      {
        path: "/jldw",
        name: "jldw",
        component: () => import("../views/jcsj/jldw.vue"),
        meta: {
          title: "计量单位",
        },
      },
      {
        path: "/bz",
        name: "bz",
        component: () => import("../views/jcsj/bz.vue"),
        meta: {
          title: "币种",
        },
      },
      {
        path: "/yhzh",
        name: "yhzh",
        component: () => import("../views/jcsj/yhzh.vue"),
        meta: {
          title: "银行账户",
        },
      },
      {
        path: "/xsfp",
        name: "xsfp",
        component: () => import("../views/fpdj/xsfp.vue"),   
        meta: {
          title: "销售发票",
        },
      },
      {
        path: "/xsfphc",
        name: "xsfphc",
        component: () => import("../views/fpdj/xsfphc.vue"),
        meta: {
          title: "销售发票(红冲)",
        },
      },
      {
        path: "/xsfpth",
        name: "xsfpth",
        component: () => import("../views/fpdj/xsfpth.vue"),
        meta: {
          title: "销售发票(退货)",
        },
      },
      {
        path: "/cgfp",
        name: "cgfp",
        component: () => import("../views/fpdj/cgfp.vue"),
        meta: {
          title: "采购发票",
        },
      },
      {
        path: "/cgfphc",
        name: "cgfphc",
        component: () => import("../views/fpdj/cgfphc.vue"),
        meta: {
          title: "采购发票(红冲)",
        },
      },
      {
        path: "/cgfpth",
        name: "cgfpth",
        component: () => import("../views/fpdj/cgfpth.vue"),
        meta: {
          title: "采购发票(退货)",
        },
      },
    ],
  },
];

const router = new VueRouter({
  routes
})
//路由导航守卫
router.beforeEach((to,from,next)=>{
  if (to.path === from.path) {
    // 如果是同一个页面，直接放行，不进行实际的路由跳转等操作，即不执行额外的代码逻辑
    next(false);
    }
  if (to.path == "/login"){
    localStorage.removeItem("Token");
    return next();
  }
  // 获取缓存中的token
  let token = localStorage.getItem("Token");
  if(!token) return next('/login')
    next()

  //to:要访问的路径
  // from：从哪个页面来
  // next()放行

})
// 路由后置守卫
router.afterEach((to,form)=>{
  // console.log(to.meta.title);
  document.title = to.meta.title;
  localStorage.setItem("title", to.meta.title);
  // Vue.prototype.$bus.$emit("update", to.meta.title);
})

export default router
